﻿using Management.Auxiliary;
using Management.Core.News;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Management.Areas.Control.Controllers
{
    [AdminAuthorize]
    public class NewsController : Controller
    {
        private NewsManager newsManager = new NewsManager();
        //
        // GET: /Control/News/
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 新闻列表
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="contentType"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public ActionResult PageList(string keywords, int? contentType, int? pageNumber, int? pageSize, int? order)
        {
            Paging<News> _pagingNews = new Paging<News>();
            if (pageNumber != null && pageNumber > 0) _pagingNews.PageIndex = (int)pageNumber + 1;
            if (pageSize != null && pageSize > 0) _pagingNews.PageSize = (int)pageSize;
            var _paging = newsManager.FindPageList(_pagingNews, keywords, contentType, null);
            return Json(new { total = _paging.TotalNumber, rows = _paging.Items.Select(n => new { n.Author, n.Title,n.NewsID,n.ContentType,n.Created_at,n.Updated_at }) });
        }

        public ActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 添加新闻
        /// </summary>
        /// post方式
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Add([Bind(Include = "NewsID, Title, Author, Content, ContentType, Created_at, Updated_at")]News _news)
        {
            _news.Created_at = DateTime.Now;
            _news.Updated_at = DateTime.Now;
            Response _resp = new Response() { Code = -1 };
            if (ModelState.IsValid)
            {
                _resp = newsManager.Add(_news);
                _resp.Code = 1;
                _resp.Message = "添加成功";
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = General.GetModelErrorString(ModelState);
            }
            return JavaScript("layer.alert('" + _resp.Message + "',function(){location.href = 'Index'})");

        }

        [HttpPost]
        public ActionResult Delete(int newsid)
        {
            Response _resp = new Response();
            var _news = newsManager.Find(newsid);
            if (_news == null)
            {
                _resp.Code = 0;
                _resp.Message = "要删除条目不存在";
            }
            else
            {
                newsManager.Delete(_news);
                _resp.Code = 1;
                _resp.Message = "删除成功";
            }
            return Json(_resp);
        }

        /// <summary>
        /// 修改新闻信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Modify(int id)
        {
            return View(newsManager.Find(id));
        }

        /// <summary>
        /// 修改新闻信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Modify(int id, FormCollection form)
        {
            Response _resp = new Response();
            var _news = newsManager.Find(id);
            _news.Updated_at = DateTime.Now;
            if (TryUpdateModel(_news, new string[] { "Title", "Author", "Content", "ContentType", "Created_at", "Updated_at" }))
            {
                newsManager.Update(_news);
                _resp.Code = 1;
                _resp.Message = "更新成功";
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = "模型不符合条件，更新失败";
            }
            return JavaScript("layer.alert('" + _resp.Message + "',function(){location.href = '../Index'})");
        }

        //[ValidateAntiForgeryToken]
        public ActionResult Detail(int id)
        {
            return View(newsManager.Find(id));
        }
	}
}